package playitas

import org.apache.solr.client.solrj.*
import org.apache.solr.client.solrj.impl.*
import org.apache.solr.client.solrj.response.*
import org.apache.solr.common.params.*
import org.apache.solr.common.util.*
import org.codehaus.groovy.grails.commons.*

class CoordenadasController {

    def index = {

        def userAgent = request.getHeader('user-agent')
        if (params.mobile ||
                userAgent =~ /(?i)iphone/ ||
                userAgent =~ /(?i)android/) {
            render view: "index_m"
        } else {
            render view: "index"
        }
    }

    def busqueda = {
        NamedList<String> parameters = new NamedList<String>();
        if (params.playa) {
            params.playa = params.playa.replaceAll("[^\\p{L} ]", "")
        } else {
            params.playa = "*:*"
        }
        parameters.add("q", params.playa)
        if (params.latitud?.isDouble() && params.longitud?.isDouble()) {
            parameters.add("pt", params.latitud + "," + params.longitud)
        }

        SolrServer sol = new CommonsHttpSolrServer(ConfigurationHolder.config.solrUrlServer);
        QueryResponse qResponse = sol.query(SolrParams.toSolrParams(parameters));

        //render qResponse as XML
        [resp: qResponse]
    }

    def playa = {
        NamedList<String> parameters = new NamedList<String>();
        parameters.add("q", "id:" + params.id)
        parameters.add("defType", "lucene")

        SolrServer sol = new CommonsHttpSolrServer(ConfigurationHolder.config.solrUrlServer);
        QueryResponse qResponse = sol.query(SolrParams.toSolrParams(parameters));

        //render qResponse as XML
        [r: qResponse.getResults().get(0)]
    }
}
